﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
/// ContentActions 的摘要说明
/// </summary>
public class ContentActions
{
    public Dictionary<string, int> Flag
    {
        get
        {
            Dictionary<string, int> flag = new Dictionary<string, int>();
            flag.Add("About", 1);
            flag.Add("Banner", 2);
            flag.Add("Product", 3);
            flag.Add("Job", 4);
            flag.Add("Contact", 5);
            flag.Add("News", 6);
            return flag;
        }
    }

    public DataTable GetByID(int id)
    {
        string sql = "select * from [temple_Contents] where [ID] = @id and [IsDel] = 0";
        SqlParameter[] pars = { new SqlParameter("@id", id) };
        return DBHelper.GetDataTable(sql, pars);
    }

    public DataTable GetAll(int cid, string lang = "chs")
    {
        string sql = "select * from [temple_Contents] where [CategoryID] = @cid and [Language] = @lang and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = {
            new SqlParameter("@cid",cid),
            new SqlParameter("@lang",lang)
        };
        return DBHelper.GetDataTable(sql, pars);
    }
    public DataTable GetAll(int cid, int flag, string lang = "chs")
    {
        string sql = "select * from [temple_Contents] where [CategoryID] = @cid and [Flag] = @flag and [Language] = @lang and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = {
            new SqlParameter("@cid",cid),
            new SqlParameter("@flag",flag),
            new SqlParameter("@lang",lang)
        };
        return DBHelper.GetDataTable(sql, pars);
    }
    public DataTable GetAll(string flag, string lang = "chs")
    {
        string sql = "select * from [temple_Contents] where [Flag] = @flag and [Language] = @lang and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = {
            new SqlParameter("@flag",flag),
            new SqlParameter("@lang",lang)
        };
        return DBHelper.GetDataTable(sql, pars);
    }
    public DataTable GetAll(int flag, string showlist, string lang)
    {
        string sql = "select * from [Contents] where [Flag] = " + flag + " and [ShowList] like '%" + showlist + "%' and [Language] = '" + lang + "' and [IsDel] = 0 order by SortID";
        return DBHelper.GetDataTable(sql);
    }

    public DataTable GetList(int cid)
    {
        string sql = "select * from [temple_Contents] where [CategoryID] = @cid and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = { new SqlParameter("@cid", cid) };
        return DBHelper.GetDataTable(sql, pars);
    }
    public DataTable GetList(string flag)
    {
        string sql = "select * from [temple_Contents] where [Flag] = @flag and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = { new SqlParameter("@flag", flag) };
        return DBHelper.GetDataTable(sql, pars);
    }
    public DataTable GetList(int cid, int flag)
    {
        string sql = "select * from [temple_Contents] where [CategoryID] = @cid and [Flag] = @flag and [IsDel] = 0 order by SortID";
        SqlParameter[] pars = { 
            new SqlParameter("@cid", cid),
            new SqlParameter("@flag", flag)
        };
        return DBHelper.GetDataTable(sql, pars);
    }
    public int GetPagersCount(int flag, string lang)
    {
        string sql = string.Format("select count([ID]) from [temple_Contents] where [Flag]={0} and [Language]='{1}' and [IsDel]=0", flag, lang);
        DataTable dt = DBHelper.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0) return int.Parse(dt.Rows[0][0].ToString());
        else return 0;
    }
    public DataTable GetPagers(int count, int startpage, int flag, string lang)
    {
        string sql = string.Format("select top {0} [rownumber], * from (select row_number() over (order by [SortID] desc) [rownumber], * from [temple_Contents] where [Flag]={2} and [Language]='{3}' and [IsDel]=0) a where [rownumber]>={1} order by sortid", count, startpage, flag, lang);
        return DBHelper.GetDataTable(sql);
    }
    public DataTable GetPagersbyKey(string key, int count, int startpage, int flag, string lang)
    {
        string sql = string.Format("select top {0} [rownumber], * from (select row_number() over (order by [SortID] desc) [rownumber], * from [temple_Contents] where [Title] like '%{4}%' and  [Flag]={2} and [Language]='{3}' and [IsDel]=0) a where [rownumber]>={1} order by sortid", count, startpage, flag, lang, key);
        return DBHelper.GetDataTable(sql);
    }
    public int AddContent(string title, string depict, string info, string photo, string att, int cid, int flag, int sort, string lang, int uid)
    {
        string sql = "insert into [temple_Contents] ([Title],[Depict],[Info],[Photo],[Attachment],[CategoryID],[Flag],[SortID],[Language],[AddUser]) values(@title,@depict,@info,@photo,@att,@cid,@flag,@sort,@lang,@uid)";
        SqlParameter[] pars = {
            new SqlParameter("@title",title),
            new SqlParameter("@depict",depict),
            new SqlParameter("@info",info),
            new SqlParameter("@photo",photo),
            new SqlParameter("@att",att),
            new SqlParameter("@cid",cid),
            new SqlParameter("@flag",flag),
            new SqlParameter("@sort",sort),
            new SqlParameter("@lang",lang),
            new SqlParameter("@uid",uid)
        };
        return DBHelper.ExecuteCommand(sql, pars);
    }

}
